/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.gateway.handler;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;

import com.hyts.infodb.gateway.bean.RouteDefinitionBO;
import com.hyts.infodb.gateway.bean.RouteDefinitionPO;
import com.hyts.infodb.gateway.bean.RouteDefinitionVO;
import com.hyts.infodb.gateway.service.GatewayService;
import com.hyts.infodb.utils.NetworkUtils;
import com.hyts.infodb.config.SpringContextConfiguration;

import static org.springframework.web.reactive.function.server.ServerResponse.ok;
import reactor.core.publisher.Mono;

/** 
 * @author LiBo/Alex
 * @version V1.0 
 */
public class GatewayViewHandler {
    
    
    /**  
     * @param serverRequest
     * @return
     * @exception
     */ 
    public static Mono<ServerResponse> index(ServerRequest serverRequest) {
        Map<String,Object> paramData = new HashMap<>();
        return ok().render("index",paramData);
    }
    
    /**  
     * @param serverRequest
     * @return
     * @exception
     */ 
    public static Mono<ServerResponse> nodeIndex(ServerRequest serverRequest) {
        GatewayService service = SpringContextConfiguration.getGlobalBean(GatewayService.class);
        RouteDefinitionVO vo = service.findGatewayDataList(new RouteDefinitionBO());
        Map<String,Object> paramData = new HashMap<>();
        NetworkUtils.getServerIpAddress();
        paramData.put("port", serverRequest.uri().getPort());
        paramData.put("ip", serverRequest.uri().getHost());
        paramData.put("data",vo.getContents());
        return ok().render("Node/index",paramData);
    }
    
    
    /**  
     * @param serverRequest
     * @return
     * @exception
     */ 
    public static Mono<ServerResponse> refresh(ServerRequest serverRequest){
        GatewayService service = SpringContextConfiguration.getGlobalBean(GatewayService.class);
        service.refreshRoute();
        RouteDefinitionVO vo = service.findGatewayDataList(new RouteDefinitionBO());
        Map<String,Object> paramData = new HashMap<>();
        NetworkUtils.getServerIpAddress();
        paramData.put("port", serverRequest.uri().getPort());
        paramData.put("ip", serverRequest.uri().getHost());
        paramData.put("data",vo.getContents());
        return ok().render("Node/index",paramData);
    }
    
    
    
    /**  
     * @param serverRequest
     * @return
     * @exception
     */ 
    public static Mono<ServerResponse> nodeAdd(ServerRequest serverRequest){
        Map<String,Object> paramData = new HashMap<>();
        return ok().render("Node/add",paramData);
    }
    
    
    /**  
     * @param serverRequest
     * @return
     * @exception
     */ 
    public static Mono<ServerResponse> nodeEdit(ServerRequest serverRequest){
        Map<String,Object> paramData = new HashMap<>();
        Optional<String> queryParam = serverRequest.queryParam("id");
        if(serverRequest.queryParam("id").isPresent()) {
            queryParam.get();
            GatewayService service = SpringContextConfiguration.getGlobalBean(GatewayService.class);
            RouteDefinitionBO routeDefinitionBO = new RouteDefinitionBO();
            routeDefinitionBO.setId(Long.parseLong(queryParam.get()));
            RouteDefinitionPO po = service.loadGatewayData(routeDefinitionBO);
            paramData.put("gateway", po);
        }
        return ok().render("Node/edit",paramData);
    }
    
}
